package com.qidianit.mes.md.service.impl;

import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.Constant;
import com.qidianit.mes.md.mapper.ClassifyMapper;
import com.qidianit.mes.md.service.ClassifyService;
import com.qidianit.mes.pojo.MaterialClassify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author 奇幻夜之梦
 * @Company 起点编程
 * @Description
 * @Time 2024/10/16 1:16
 */
@Service
public class ClassifyServiceImpl implements ClassifyService {
    @Autowired
    private ClassifyMapper classifyMapper;
    @Override
    public MESResult save(MaterialClassify materialClassify) {
        if (materialClassify.getClassifyName() == null || materialClassify.getClassifyName() == "") {
            return MESResult.error("分类名称不能为空");
        }
        if (materialClassify.getClassifyOrder() == null) {
            return MESResult.error("排序不能为空");
        }
        if (materialClassify.getClassifyType() == null) {
            return MESResult.error("分类是物料还是产品选其一");
        }
        MaterialClassify tempClassify = classifyMapper.selectByName(materialClassify.getClassifyName());
        if (tempClassify != null)
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT,"分类名称重复");

        materialClassify.setCreateTime(new Date());
        materialClassify.setModifyTime(new Date());
        int i = classifyMapper.save(materialClassify);
        // 判断当前任务是否有父级任务
        if(i == 1 && !materialClassify.getClassifyParentId().equals(new Long(0))){
            // 查询父级任务
            MaterialClassify parent = classifyMapper.selectById(materialClassify.getClassifyParentId());
            // 判断父级任务的parent是否是1
            if(parent.getClassifyParent().equals(new Integer(2))){
                parent.setClassifyParent(1);
                classifyMapper.updateById(parent);
            }
        }
        return i == 1?MESResult.success(materialClassify):MESResult.error();
    }

    @Override
    public MESResult list(MaterialClassify materialClassify, Long parentId) {
        return MESResult.success(classifyMapper.selectList(materialClassify,parentId));
    }

    @Override
    public MESResult selectById(Long classifyId) {
        return MESResult.success(classifyMapper.selectById(classifyId));
    }

    @Override
    public MESResult updateClassify(MaterialClassify materialClassify) {
        if (materialClassify.getClassifyName() == null || materialClassify.getClassifyName() == ""){
            return MESResult.error("分类名称不能为空");
        }
        if (materialClassify.getClassifyOrder() == null) {
            return MESResult.error("排序不能为空");
        }
        if (materialClassify.getClassifyType() == null) {
            return MESResult.error("分类是物料还是产品选其一");
        }
        // 获取原任务信息
        MaterialClassify originalClassify = classifyMapper.selectById(materialClassify.getClassifyId());

        MaterialClassify classifyTempName = classifyMapper.selectByName(materialClassify.getClassifyName());
        if (classifyTempName != null && !classifyTempName.getClassifyName().equals(originalClassify.getClassifyName())) {
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT,"分类名称重复");
        }

        return classifyMapper.updateById(materialClassify) == 1?MESResult.success():MESResult.error();
    }

    @Override
    public MESResult countSubclassify(Long classifyId) {
        return MESResult.success(Constant.RESPONSE_STATUS_OK,"OK",classifyMapper.countSubclassify(classifyId));
    }

    @Override
    public MESResult delete(Long classifyId) {
        MaterialClassify materialClassify = new MaterialClassify();
        materialClassify.setClassifyId(classifyId);
        // 修改状态
        materialClassify.setClassifyStatus(Constant.DATA_STATUS_DELETE);
        return MESResult.isSuccess(classifyMapper.updateById(materialClassify));
    }

    @Override
    public MESResult getTree(Long parentId) {
        List<MaterialClassify> materialClassifies = get(parentId);
        return MESResult.success(materialClassifies);
    }
    public List<MaterialClassify> get(Long parentId){
        List<MaterialClassify> materialClassifies = queryClassifyByParentId(parentId);
        return materialClassifies;
    }
    private List<MaterialClassify> queryClassifyByParentId(Long parentId){
        List<MaterialClassify> materialClassifies = classifyMapper.selectByParentId(parentId);
        if(materialClassifies == null || materialClassifies.size() == 0){
            return null;// 通过parentId没有查询到子菜单，就结束
        }
        // 判断每一个菜单是否有子菜单
        for (MaterialClassify materialClassify: materialClassifies){
            if(materialClassify.getClassifyParent().equals(new Integer(1))){
                // 说明有子菜单，查询子菜单
                // 递归调用自己查询，将自己的id作为父菜单id传入
                List<MaterialClassify> subClassifies = get(materialClassify.getClassifyId());
                // 将子菜单设置到children中
                materialClassify.setChildren(subClassifies);
            }
        }
        return materialClassifies;
    }
}

